<?php

namespace App\Console\Commands;

use App\Events\LoanRequestApplyEvent;
use App\Facades\SamLogFacade;
use App\Models\UserLoanRequest;
use Carbon\Carbon;
use Illuminate\Console\Command;

class CheckRetryLoanRequest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check:loan_request_retry';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'find need to retry loan request';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $list = UserLoanRequest::where(
            [
                'retry'=>UserLoanRequest::RETRY_YES,'retry_date'=>Carbon::now()->toDateString(),
                'status'=>2,'auto_loan_apply'=>0
            ]
        )->get();
        if($list->count()>0){
            SamLogFacade::debug("查询到需要重试授信的数据{$list->count()}");
            foreach ($list as $index => $loan){
                SamLogFacade::debug("本次列表第{$index}个：{$loan->id}");
                SamLogFacade::debug("具体数据：".json_encode($loan,JSON_UNESCAPED_UNICODE));
                event(new LoanRequestApplyEvent($loan));
            }
        }

    }
}
